// Implement atoi to convert a string to an integer.
// 
// Hint: Carefully consider all possible input cases. 
// If you want a challenge, please do not see below and ask yourself what are the possible input cases.
// 
// Notes: It is intended for this problem to be specified vaguely (ie, no given input specs).
//  You are responsible to gather all the input requirements up front.

class Solution {
public:
int myAtoi(string str)
{

	long i = 1;
	int ret = 0;
	int size = str.size();

	if (size == 0)
		return 0;

	while (size - 1)
	{
		if (str[size - 1] < 48 || str[size - 1] > 57)
			return 0;

		ret = (str[size - 1] - 48)*i + ret;
		if (ret > INT_MAX)
			return 0;

		size--;
		ret = i * 10 ;
	}

	if (str[0] < 48 || str[0] > 57)
	{
		if (str[0] == 43)
			return ret;

		if (str[0] == 45)
			return ret*-1;

		return 0;
	}


	ret = (str[0] - 48)*i+ret;
	if (ret > INT_MAX)
		return 0;

	return ret;
}
};